<template>
    <div>
        <el-table :data="menulist" row-key="id" border default-expand-all :tree-props="{ children: 'children' }"
            style="width: 100%">
            <el-table-column prop="id" label="菜单编号" />
            <el-table-column prop="title" label="菜单名称" />
            <el-table-column prop="pid" label="上级菜单" />
            <el-table-column prop="icon" label="菜单图标">
                <template #default="item">
                    <div>
                        <el-icon>
                            <component :is="item.row.icon"></component>
                        </el-icon>
                    </div>
                </template>
            </el-table-column>
            <el-table-column prop="url" label="菜单地址" />
            <el-table-column label="状态">  
                <template #default="item">
                    <div>
                        <el-tag type="success" v-if="item.row.status == 1">启用</el-tag>
                        <el-tag type="danger" v-else>禁用</el-tag>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="操作">
                <template #default="item">
                    <div>
                        <el-button type="primary" size="small" @click="edit(item.row.id)">编辑</el-button>
                        <el-button type="danger" size="small" @click="del(item.row.id)">删除</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
    </div>
</template>

<script lang="ts" setup>
import { menuDel } from '@/request/api'
import { ElMessage, ElMessageBox } from 'element-plus'
import { defineEmits, computed } from 'vue';

import { useStore } from 'vuex';
const store = useStore()
console.log(store, '仓库');
const menulist = computed(() => store.getters['menu/getMenuList'])
// console.log(menulist, 'menulist');

const getMenu = () => {
    store.dispatch('menu/MenuListActions')
}
getMenu()


let emit = defineEmits(['edit'])
const edit = (id: number) => {
    emit('edit', id)
}

let del = (id: number) => {
    ElMessageBox.confirm(
        '你确定要删除这条数据吗?',
        '提示警告',
        {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(() => {
            menuDel({
                id
            }).then((res: any) => {
                console.log(res, '菜单删除');
                if (res.code == 200) {
                    ElMessage({
                        type: 'success',
                        message: res.msg
                    })
                    getMenu()
                    return
                }
                ElMessage({
                    type: 'info',
                    message: res.msg
                })
            })
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '取消删除',
            })
        })
}

</script>

<style scoped>
</style>
